#!/bin/bash
#
# USAGE:
#       p multiplePairs_Parallel.adr inputFile
#
# DESCRIPTION:
#  multiplePairs_Parallel.adr is an ADORE script.  
#  It generates several interferograms whose master and slave orbits are defined
#  by the input file. 
#  This script uses the Torque-PBS system to distribute jobs to other computers.
#
# INPUT:
#  inputFile: A comma-separated-value file where each line indicates an
#    interferometric pair. First value defines the master and the second value defines the slave.
#    As an example file should have this format:
#    masterScene1,slaveScene1
#    masterScene2,slaveScene2
#    ...
#  
# OUTPUT:
#  Creates ${projectFolder}/process/${runName}/ and processes crops & interferograms
#  It runs the following DORIS steps:
#    For crops: m_readfiles, m_porbits, m_crop
#    For i12s: coarseorb, coarsecorr, fine, coregpm, resample, interfero, comprefpha, subtrrefpha, coherence
#  It also creates an amplitude raster for resample and a phase raster for subtrrefpha.
#

# The following steps are processed for each crop
cropSteps="m_readfiles;m_porbits;m_crop;"
# The following steps are processed for each interferogram
interferoSteps="coarseorb;coarsecorr;fine;coregpm;resample;interfero;comprefpha;subtrrefpha;coherence;raster auto coherence -- -M4/4;raster p subtrrefpha -- -M4/4"
#Options for quejob/qsub 
qsubOptions="" #"-l nodes=demeter:ppn=1" 
#######################################################
## DO NOT CHANGE ANYTHING BELOW
## UNLESS YOU KNOW WHAT YOU ARE DOING.
#######################################################
function processCropSteps(){
for scene in `cat ${pairsFile} | tr "," "\n" | sort --unique`
do
     settings load ${originalSettingsFile} 
     bold "Processing ${scene}"
     settings apply -r -q master=${scene} slave=dummy outputFolder="${cropsFolder}/${scene}"
     mkdir -p "${outputFolder}"
     cd "${outputFolder}"    
     settings save

     #parallelProcess "${outputFolder}" "${cropSteps}"
     jobId=`quejob "${cropSteps}" "${outputFolder}" -- ${qsubOptions}`
     jobIds=( ${jobIds[@]} ${jobId} )
done
}

function processInterferoSteps(){
for scenes in `cat ${pairsFile}`
do
   settings load ${originalSettingsFile} 
   master=${scenes%%,*}
   scene=${scenes#*,}   
   if [ "${master}" == "${scene}" ]; then
     continue
   fi
   bold "Processing ${master}_${scene}"
   settings apply -r -q master=${master} slave=${scene} outputFolder="${i12sFolder}/${master}_${scene}"
   mkdir -p "${outputFolder}"
   cd "${outputFolder}"
   settings save
   cp ${cropsFolder}/${master}/${master}.res ${outputFolder}/${master}.res
   cp ${cropsFolder}/${slave}/${slave}.res ${outputFolder}/${slave}.res

   jobId=`quejob "${interferoSteps}" "${outputFolder}" -- ${qsubOptions}`
   jobIds=( ${jobIds[@]} ${jobId} )
done
}

local CALLDIR=$PWD
local pairsFile=`canonicalPath ${1}`
# we will be changing directories
# save the current dir.

generateRandomString
local originalSettingsFile="${CALLDIR}/${randomString}.set"
#save current settings so that we can recall them at the end.
settings save ${originalSettingsFile}

jobIds=( ); #start empty list
processCropSteps
waitjob ${jobIds[@]}
 
####Now the i12s
#Set the stack master.
jobIds=( ); #start empty list
cd ${CALLDIR} #if pairs file is given by relative path we want to be in CALLDIR.
processInterferoSteps

#go back to where we started
cd ${CALLDIR}
echo "Replacing user settings"
settings load ${originalSettingsFile}
rm ${originalSettingsFile}
bold "Parallel job submission is complete."
echo "You can CTRL+C the counter to get the prompt back."
echo "However please keep in mind that jobs are still running."
waitjob ${jobIds[@]}
